//
//  LGNoteCutImageViewController.m
//  NoteDemo
//
//  Created by hend on 2019/3/26.
//  Copyright © 2019 hend. All rights reserved.
//

#import "LGNCutImageViewController.h"
#import "LGNImageView.h"
#import "LGNDrawBoardViewController.h"
#import "LGKConst.h"
@interface LGNCutImageViewController ()
/** 裁剪图片使用 */
@property (nonatomic, strong) LGNImageView *cutImageView;
@property (nonatomic, strong) UIButton *sureBtn;
@property (nonatomic, strong) UIButton *cancelBtn;

@property (nonatomic,assign) BOOL  isHeng;
@end

@implementation LGNCutImageViewController

- (void)dealloc{
    NSLog(@"%@释放了",NSStringFromClass([self class]));
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    [self createSubViews];
    
    CGFloat width = _image.size.width;
    CGFloat height = _image.size.height;
    
    if(width > height  && _isCamera){
        
        _isHeng = YES;
    }else{
        _isHeng = NO;
    }
    
 
}

- (void)createSubViews{
    [self.view addSubview:self.cutImageView];
    [self.view addSubview:self.cancelBtn];
    [self.view addSubview:self.sureBtn];
    
    [self setupSubViewContraints];
}

- (void)setupSubViewContraints{
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(LG_IS_IPHONEX ? -20 : -10);
        make.right.equalTo(self.view).offset(-15);
        make.size.mas_equalTo(CGSizeMake(64, 28));
    }];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.sureBtn);
        make.left.equalTo(self.view).offset(15);
        make.size.mas_equalTo(CGSizeMake(50, 28));
    }];
    [self.cutImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset((LG_IS_IPHONEX ? 44.0f : 20.0f));
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.cancelBtn.mas_top);
    }];
}


- (void)useImage:(UIButton *)sender{
    
//    LGNDrawBoardViewController *drawController = [[LGNDrawBoardViewController alloc] init];
//    drawController.style = LGNoteDrawBoardViewControllerStyleDefault;
//
//
//    drawController.drawBgImage = [self.cutImageView currentCroppedImage];
//    drawController.size = [self.cutImageView currnetImageSize];
//
//   drawController.isHeng = _isHeng;
//   drawController.modalPresentationStyle = UIModalPresentationFullScreen;
    
//    [self presentViewController:drawController animated:YES completion:nil];
    LGNDrawBoardViewController *drawBoardVC = (LGNDrawBoardViewController *)self.presentingViewController;
    if (drawBoardVC.drawResultBlock){
        drawBoardVC.drawResultBlock([self.cutImageView currentCroppedImage]);
    }
    [drawBoardVC.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

- (void)cancelUseImage:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - lazy
- (LGNImageView *)cutImageView{
    if (!_cutImageView) {
        _cutImageView = [[LGNImageView alloc] init];
        // 裁剪图片对象
   
        _cutImageView.toCropImage = self.image;
        // 是否显示中间线
        _cutImageView.showMidLines = YES;
        // 是否需要缩放裁剪
        _cutImageView.needScaleCrop = YES;
        // 是否显示九宫格交叉线
        _cutImageView.showCrossLines = NO;
        // 边角是否需要裁剪
        _cutImageView.cornerBorderInImage = NO;
        // 裁剪区域边界线宽
        _cutImageView.cropAreaCornerWidth = 44;
        //裁剪区域边界高度
        _cutImageView.cropAreaCornerHeight = 44;
        // 裁剪区域相邻两个角度的最小间距
        _cutImageView.minSpace = 30;
        // 裁剪区圆角线颜色
        _cutImageView.cropAreaCornerLineColor = [UIColor whiteColor];
        // 裁剪边线
        _cutImageView.cropAreaBorderLineColor = [UIColor whiteColor];
        _cutImageView.cropAreaCornerLineWidth = 3;
        _cutImageView.cropAreaBorderLineWidth = 2;
        // 裁剪区域中间线的宽度
        _cutImageView.cropAreaMidLineWidth = 20;
        // 裁剪区域中间线的高度
        _cutImageView.cropAreaMidLineHeight = 6;
        _cutImageView.cropAreaMidLineColor = [UIColor whiteColor];
        _cutImageView.cropAreaCrossLineColor = [UIColor whiteColor];
        // 裁剪区域交叉线的宽度
        _cutImageView.cropAreaCrossLineWidth = 4;
        _cutImageView.initialScaleFactor = .8f;
    }
    return _cutImageView;
}

- (UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [[UIButton alloc] init];
        [_sureBtn setTitle:@"完成" forState:UIControlStateNormal];
        _sureBtn.layer.cornerRadius = 14.f;
        _sureBtn.layer.masksToBounds = YES;
        [_sureBtn setBackgroundColor:LG_ColorWithHex(0x0099ff)];
        [_sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sureBtn addTarget:self action:@selector(useImage:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

- (UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [[UIButton alloc] init];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancelUseImage:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}


@end
